package com.coastline.fruit.common.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * @author Coastline
 * @date 2025/1/29 22:14
 */
@Component
public class ElasticsearchClient {
    private static RestHighLevelClient esClient;

    @PostConstruct
    private void buildEsClient() {
        esClient = new RestHighLevelClient(
                RestClient.builder(HttpHost.create("http://192.168.73.200:9200")));
    }

    public RestHighLevelClient getInstance() {
        return this.esClient;
    }


    public void closeEsClient() throws IOException {
        if (this.esClient != null) {
            this.esClient.close();
        }
    }
}
